//
//  BwebviewViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/6/14.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BwebviewViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "BinvitiveViewController.h"
#import "BinSHopViewController.h"
#import "JSHAREService.h"
#import <AdSupport/AdSupport.h>
#import "ShareView.h"
#import "BInviteManagerViewController.h"

@interface BwebviewViewController ()<UIWebViewDelegate>
@property(nonatomic, strong)UIWebView *webView;
@property(nonatomic, strong)NSString *urlString;

@end

@implementation BwebviewViewController

- (UIWebView *)webView {
    if (!_webView) {
        self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
    }
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"邀请加入";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    //[self.rightbutton setTitle:@"分享" forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor whiteColor];
    //if (strIsEmpty(_urlString)) {
    //    _urlString = @"http://192.168.2.123:8848/%E7%BD%91%E9%A1%B5/new_file.html?";
    //}
    
    if (strIsEmpty(_urlString)) {
        //NSString *steam  = @"/%E7%BD%91%E9%A1%B5";
        _urlString = [NSString stringWithFormat:@"%@?id=%@",Binvite,KUID];
        NSLog(@"%@", _urlString);
        
    }
    //_urlString = @"http://192.168.2.123:8848/%E7%BD%91%E9%A1%B5/new_file.html#?id=122";
    self.webView = [[UIWebView alloc]initWithFrame:self.view.frame];
    
    KMyLog(@"dfadsfdsa%@",_urlString);
    self.webView.scalesPageToFit = YES;
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
    self.webView.delegate = self;
    
    [MBProgressHUD showHUDAddedTo:self.view animated: YES];
    // Do any additional setup after loading the view.
}

- (void)share {
    
    NSMutableArray *titlearr     = [NSMutableArray arrayWithCapacity:5];
    NSMutableArray *imageArr     = [NSMutableArray arrayWithCapacity:5];
    NSMutableArray *typeArr     = [NSMutableArray arrayWithCapacity:5];
    [titlearr addObjectsFromArray:@[@"微信", @"微信朋友圈"]];
    [imageArr addObjectsFromArray:@[@"weixin22",@"weixin11"]];
    
    ShareView *shareView = [[ShareView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"分享到"];
    [shareView setBtnClick:^(NSInteger btnTag) {
        NSLog(@"\n点击第几个====%d\n当前选中的按钮title====%@",(int)btnTag,titlearr[btnTag]);
        [self shareWitipe:btnTag];
    }];
    [[UIApplication sharedApplication].keyWindow addSubview:shareView];
}

-(void)shareWitipe:(NSInteger )ind{
    ind++;
    //[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *typestr = ind ==0?@"JSHAREPlatformWechatSession":@"JSHAREPlatformWechatTimeLine";
    
    NSArray *type = @[@"JSHAREPlatformWechatSession",@"JSHAREPlatformWechatTimeLine"];
    
    JSHAREMessage *message = [JSHAREMessage message];
    message.mediaType = JSHARELink;
    NSString *invitation_code =[NSString stringWithFormat:@"%@",[USER_DEFAULT objectForKey:@"invitation_code"]];
    NSString *invitUrl = [NSString stringWithFormat:@"https://www.zzzsyh.com/index.html#/B_joining?invitationCode=%@", invitation_code];
    NSLog(@"%@", invitUrl);
    message.url = invitUrl;
    message.text = [NSString stringWithFormat:@"侯保师傅接单APP注册:%@",[self localizedStringTime]];
    message.title = @"主事丫环电器维修服务平台邀您一起赚钱";
    message.platform = ind;
    //NSString *imageURL = @"http://img2.3lian.com/2014/f5/63/d/23.jpg";
    
    NSString *imageURL = @"http://a2.qpic.cn/psb?/V13oNyH23yqWNu/No5HO5K36pnJQvTKVWBRo6SpHxNpX5jpYFVnJPOsCgM!/c/dDUBAAAAAAAA&ek=1&kp=1&pt=0&bo=OAQ4BAAAAAADJwI!&tl=1&vuin=374827167&tm=1564729200&sce=60-2-2&rf=newphoto&t=5";
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
    message.image = imageData;
    [JSHAREService share:message handler:^(JSHAREState state, NSError *error) {
        //[self showAlertWithState:state error:error];
        KMyLog(@"sdf%@",error);
        //[MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            ShowToastWithText(@"分享成功");
        } else {
            ShowToastWithText(@"分享失败");
            NSLog(@"分享失败, error : %@", error);
        }
    }];
}

- (NSString *)localizedStringTime{
    NSDateFormatter*formatter = [[NSDateFormatter alloc]init];[formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
    NSString*dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    //    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //    //定义好JS要调用的方法, share就是调用的share方法名
    //    context[@"onFailDate"] = ^() {
    //        NSLog(@"+++++++Begin Log+++++++");
    //        NSArray *args = [JSContext currentArguments];
    //
    //        dispatch_async(dispatch_get_main_queue(), ^{
    //            ShowToastWithText(@"调用成功");
    //        });
    //
    //        for (JSValue *jsVal in args) {
    //            NSLog(@"%@", jsVal.toString);
    //        }
    //        NSLog(@"-------End Log-------");
    //    };
    JSContext *content = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    NSLog(@"%@", content);
    
    //复制
    content[@"onfuzhi"] = ^() {
        NSLog(@"js调用oc---------begin--------复制");
        dispatch_async(dispatch_get_main_queue(), ^{
            ShowToastWithText(@"复制成功");
            NSString *str =[NSString stringWithFormat:@"%@",[USER_DEFAULT objectForKey:@"invitation_code"]];
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = str;
        });
    };
    //邀请师傅
    content[@"onlook"] = ^() {
        NSLog(@"js调用oc---------begin--------邀请师傅");
        dispatch_async(dispatch_get_main_queue(), ^{
            [self share];
        });
    };
    //查看师傅
    content[@"onyaoqing"] = ^() {
        NSLog(@"js调用oc---------begin--------查看师傅");
        dispatch_async(dispatch_get_main_queue(), ^{
            BinvitiveViewController *VC = [[BinvitiveViewController alloc]init];
            VC.index = 0;
            [self.navigationController pushViewController:VC animated:YES];
        });
    };
    
    //邀请管理商
    content[@"onG_yaoqing"] = ^() {
        NSLog(@"js调用oc---------begin--------邀请管理商");
        dispatch_async(dispatch_get_main_queue(), ^{
            BInviteManagerViewController *VC = [BInviteManagerViewController new];
            [self.navigationController pushViewController:VC animated:YES];
        });
    };
    
    //查看管理商
    content[@"onG_look"] = ^() {
        NSLog(@"js调用oc---------begin--------查看管理商");
        dispatch_async(dispatch_get_main_queue(), ^{
            BinvitiveViewController *VC = [[BinvitiveViewController alloc]init];
            VC.index = 1;
            [self.navigationController pushViewController:VC animated:YES];
        });
    };
    
    //邀请实体店
    content[@"onS_yaoqing"] = ^() {
        NSLog(@"js调用oc---------begin--------邀请实体店");
        dispatch_async(dispatch_get_main_queue(), ^{
            BinSHopViewController *VC = [BinSHopViewController new];
            [self.navigationController pushViewController:VC animated:YES];
        });
    };
    
    //查看实体店
    content[@"onS_look"] = ^() {
        NSLog(@"js调用oc---------begin--------查看实体店");
        dispatch_async(dispatch_get_main_queue(), ^{
            BinvitiveViewController *VC = [[BinvitiveViewController alloc]init];
            VC.index = 2;
            [self.navigationController pushViewController:VC animated:YES];
        });
    };
    
    //分享下载
    content[@"onshare"] = ^() {
        NSLog(@"js调用oc---------begin--------分享下载");
        dispatch_async(dispatch_get_main_queue(), ^{
            [self share];
        });
    };
}

@end
